package edu.cueb.wmis.service.impl;

import edu.cueb.wmis.pojo.Admin;
import edu.cueb.wmis.pojo.Menu;
import edu.cueb.wmis.mapper.MenuMapper;
import edu.cueb.wmis.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {
        Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        HashOperations hashOperations = redisTemplate.opsForHash();
//        ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();
//        //从redis中获取菜单数据
//        List<Menu> menus = (List<Menu>)valueOperations.get("menu_" + adminId);
//        System.out.println(menus);
//        //如果为空去数据库中查询
//        if(CollectionUtils.isEmpty(menus)){
//            menus = menuMapper.getMenusByAdminId(adminId);
//            //存入redis
//            try {
//                valueOperations.set("menu_"+adminId , menus);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return menus;
//       Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        System.out.println(adminId);
        return menuMapper.getMenusByAdminId(adminId);
    }

    @Override
    public List<Menu> getMenusByRole() {
        return menuMapper.getMenusByRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }
}
